﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#include "CellStableHeaders.h"
#include "CellUtil.h"
#include "CellStringConverter.h"

#if CELL_COMPILER == CELL_COMPILER_MSVC
	#include <windows.h>
	#include <ctime>
	#if _MSC_VER >= 1400
		#include <intrin.h>
	#endif
	#elif CELL_COMPILER == CELL_COMPILER_GNUC
	#include <signal.h>
	#include <setjmp.h>

	#if CELL_CPU == CELL_CPU_ARM
		#include <sys/sysctl.h>
		#include <mach/machine.h>
	#endif
#endif

namespace cell
{

static const char SEQUENCE_TABLE[] = {'0', '1', '2', '3', '4', '5',
	'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
static const char ALPHABET[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
	'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
	'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
	'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
#define ALPHABET_LENGTH 52

static int seedAssistCount = 0;

//-----------------------------------------------------------------------
Util::Util(void)
{
}
//-----------------------------------------------------------------------
Util::~Util(void)
{
}
//-----------------------------------------------------------------------
ulong Util::hashString(char* string)
{
	char* c = string;
	int off = 0;
	size_t len = strlen(c);
	ulong h = 0;
	for (size_t i = 0; i < len; ++i)
	{
		h = 31 * h + c[off++];
	}
	return h;
}
//-----------------------------------------------------------------------
ulong Util::hashString(const String& string)
{
	size_t bufsize = string.size();
	char* val = CELL_ALLOC_T(char, bufsize + 1, MEMCATEGORY_GENERAL);
	memset(val, 0x0, bufsize + 1);
	memcpy(val, string.c_str(), bufsize);

	ulong h = 0;
	int off = 0;
	for (size_t i = 0; i < bufsize; i++)
	{
		h = 31 * h + val[off++];
	}

	CELL_FREE(val, MEMCATEGORY_GENERAL);
	return h;
}
//-----------------------------------------------------------------------
void Util::resetSeed(void)
{
	++seedAssistCount;

	time_t t;
	::srand((unsigned) time(&t) + seedAssistCount);

	if (seedAssistCount > 1981)
		seedAssistCount = 1;
}
//-----------------------------------------------------------------------
int Util::randomInt(int floor, int ceiling)
{
	return floor + ::rand() % (ceiling - floor + 1);
}
//-----------------------------------------------------------------------
float Util::randomFloat(float floor, float ceiling)
{
	return floor + (double)::rand() / ((double)RAND_MAX + 1) * (ceiling - floor);
}
//-----------------------------------------------------------------------
String Util::randomString(int length)
{
	resetSeed();
	const int ceiling = ALPHABET_LENGTH - 1;
	String ret = "";
	for (int i = 0; i < length; ++i)
	{
		int pos = randomInt(0, ceiling);
		ret += ALPHABET[pos];
	}
	return ret;
}
//-----------------------------------------------------------------------
uint64 Util::currentTimeMillis(void)
{
#if CELL_PLATFORM == CELL_PLATFORM_WIN32
	static const uint64 magic = 116444736000000000ll; // 1970-01-01 00:00:00 - 1601-01-01 00:00:00
	SYSTEMTIME st;
	GetSystemTime(&st);
	FILETIME ft;
	SystemTimeToFileTime(&st, &ft);	// in 100-nanosecs...
	uint64 t;
	memcpy(&t, &ft, sizeof(t));
	return (t - magic)/10000;		// scale to millis.
#else
	boost::posix_time::ptime time_now(boost::posix_time::microsec_clock::local_time());
	boost::posix_time::ptime time_begin(boost::gregorian::date(1970, 1, 1),
		boost::posix_time::time_duration(0, 0, 0, 0));
	boost::posix_time::time_duration timestamp = time_now - time_begin;
	return time_duration.total_milliseconds();
#endif
}
//-----------------------------------------------------------------------
void Util::convertGB2312ToUTF8(char* out, const char* text)
{
	int len = ::MultiByteToWideChar(CP_ACP, 0, text, -1, NULL, 0);

	wchar_t* wstr = CELL_ALLOC_T(wchar_t, len + 1, MEMCATEGORY_GENERAL);
	memset(wstr, 0x0, len + 1);

	::MultiByteToWideChar(CP_ACP, 0, text, -1, wstr, len);

	len = ::WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);

	char* str = CELL_ALLOC_T(char, len + 1, MEMCATEGORY_GENERAL);
	memset(str, 0x0, len+1);

	::WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);

	memcpy(out, str, len + 1);

	CELL_FREE(wstr, MEMCATEGORY_GENERAL);
	CELL_FREE(str, MEMCATEGORY_GENERAL);
}
//-----------------------------------------------------------------------
void Util::convertUTF8ToGB2312(char* out, const char* text)
{
	int len = ::MultiByteToWideChar(CP_UTF8, 0, text, -1, NULL, 0);

	wchar_t* strSrc = CELL_ALLOC_T(wchar_t, len + 1, MEMCATEGORY_GENERAL);
	memset(strSrc, 0x0, len + 1);

	::MultiByteToWideChar(CP_UTF8, 0, text, -1, strSrc, len);

	len = ::WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);

	char* str = CELL_ALLOC_T(char, len + 1, MEMCATEGORY_GENERAL);
	memset(str, 0x0, len + 1);

	::WideCharToMultiByte(CP_ACP, 0, strSrc, -1, str, len, NULL, NULL);

	memcpy(out, str, len + 1);

	CELL_FREE(strSrc, MEMCATEGORY_GENERAL);
	CELL_FREE(str, MEMCATEGORY_GENERAL);
}
//-----------------------------------------------------------------------
inline uint Util::max(uint a, uint b)
{
	return ((a > b) ? a : b);
}
//-----------------------------------------------------------------------
inline uint Util::min(uint a, uint b)
{
	return ((a < b) ? a : b);
}

} // end namespace cell
